import { ImageAsset, SpriteFrame, TextAsset, Texture2D, _decorator } from 'cc';
import ResManager from './ResManager';
import { MapZone } from '../entity/MapZone';
const { ccclass, property } = _decorator;

@ccclass('TilesManager')
export class TilesManager {
    static configUrl: string = 'https://pics.fzzhs.com/kow/tiles.txt'
    static tilesImgUrl: string = 'https://pics.fzzhs.com/kow/tilesImg/'
    
    static _tilesData: string[] = []
    /**
     * 获取地形配置文件
     * @returns 
     */
    static async getTilesConfig() {

        if (this._tilesData.length > 0) {
            return this._tilesData
        }


        // let txt = await ResManager.instance.loadRemoteByUrl<TextAsset>(this.configUrl)
        let txt = await ResManager.instance.loadAssetByUrl<TextAsset>('tilesConfig/tiles')

        const text: string = txt.text

        this._tilesData = text.split(';')


        return this._tilesData
    }

    /**
     * 获取地形图片
     * @param x 
     * @param y 
     */
    static async getTilesImg(x: number, y: number): Promise<SpriteFrame> {
        let zoneId = MapZone.getZoneIdByXy(x, y)
        let data = await this.getTilesConfig()
        let txt = data[zoneId - 1]
        let imgList = txt.split('')
        let img = imgList.join('_')


        // let imageAsset = await ResManager.instance.loadRemoteByUrl<ImageAsset>(`${this.tilesImgUrl}${img}.png`)
        // const spriteFrame = new SpriteFrame();
        // const texture = new Texture2D();
        // texture.image = imageAsset;
    
        // spriteFrame.texture = texture;
        
        // return spriteFrame


        let sf = await ResManager.instance.loadAssetByUrl<SpriteFrame>(`tilesImg/${img}/spriteFrame`)
        return sf
    }
}


